package Connection;

import BasicClass.User;
import DAO.AccountDao;
import DAO.DaoException;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AccountDaoImpl implements AccountDao{
    public MyConnection myConnection;
    public AccountDaoImpl(MyConnection myConnection){this.myConnection = myConnection;}

    @Override
    public void addAccount(User account) throws DaoException {

    }

    @Override
    public void updateAccount(User account, int choose) throws DaoException {
        String sql  ="UPDATE account SET A_Password = ? WHERE A_Number = ?";
        String allsql = "UPDATE account SET A_Password = ?, A_Grade = ? WHERE A_Number = ?";
        String allsql2 = "UPDATE college SET C_Name = ?, C_Sexy = ?, C_Class = ?, C_Title = ?, C_Faction = ? WHERE C_Number =?";
        if(choose==1) {
            try {
                PreparedStatement pstmt = myConnection.getConnection().prepareStatement(sql);
                pstmt.setString(1, account.getPassword());
                pstmt.setString(2, account.getNumber());
                pstmt.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        else{
            try {
                PreparedStatement preparedStatement = myConnection.getConnection().prepareStatement(allsql);
                preparedStatement.setString(1, account.getPassword());
                preparedStatement.setString(2, account.getGrade());
                preparedStatement.setString(3, account.getNumber());
                preparedStatement.executeUpdate();
                preparedStatement = myConnection.getConnection().prepareStatement(allsql2);
                preparedStatement.setString(1, account.getName());
                preparedStatement.setString(2, account.getSexy());
                preparedStatement.setString(3, account.getDiscipline());
                preparedStatement.setString(4, account.getTitle());
                preparedStatement.setString(5, account.getFaction());
                preparedStatement.setString(6, account.getNumber());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public void deleteAccount(User account) throws DaoException {
        String sql = "DELETE FROM account WHERE A_Number = ?";
        try{
            PreparedStatement preparedStatement = myConnection.getConnection().prepareStatement(sql);
            preparedStatement.setString(1, account.getNumber());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User getAccount(User account) throws DaoException {
        String user = "SELECT * FROM V_account WHERE A_Number = ?";
        try {
            PreparedStatement pstmt_user = getConnection().prepareStatement(user);
            pstmt_user.setString(1, account.getNumber());
            ResultSet rst_user = pstmt_user.executeQuery();
            if(rst_user.next()) {
                account.setNumber(rst_user.getString(1));
                account.setName(rst_user.getString(2));
                account.setPassword(rst_user.getString(3));
                account.setSexy(rst_user.getString(4));
                account.setDiscipline(rst_user.getString(5));
                account.setTitle(rst_user.getString(6));
                account.setFaction(rst_user.getString(7));
                account.setGrade(rst_user.getString(8));
            }
            else account.setNumber("账号不存在");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return account;
    }

    @Override
    public ArrayList<User> getAllAccount() throws DaoException {
        ArrayList<User> arrayList = new ArrayList<>();
        String sql = "SELECT * FROM v_account";
        ResultSet rst;
        try{
            PreparedStatement pstmt = myConnection.getConnection().prepareStatement(sql);
            rst = pstmt.executeQuery();
            while (rst.next()){
                User user = new User(
                        rst.getString(1), rst.getString(2),
                        rst.getString(3), rst.getString(4),
                        rst.getString(5), rst.getString(6),
                        rst.getString(7), rst.getString(8)
                );
                arrayList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return arrayList;
    }
}
